﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web = System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using System.Reflection;
using System.ComponentModel;

namespace JQuery.Net.UI
{
    public enum MenuItemType
    {
        MenuItem = 0,
        Seperator
    }

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ParseChildren(true), PersistChildren(false)]
    public sealed class MenuItem
        : ICloneable
    {
        private MenuItemCollection items;
        #region constructors
        public MenuItem()
        {
            Items = new MenuItemCollection(this);
            /// Defaults
            Url = "#";
            Target = "_self";
        }

        public MenuItem(string text)
            : this()
        {
            Text = text;
        }

        public MenuItem(string text, string arguments)
            : this(text)
        {
            Arguments = arguments;
        }

        public MenuItem(string text, string arguments, string image)
            : this(text, arguments)
        {
            Image = image;
        }

        public MenuItem(string text, string arguments, string image, string url)
            : this(text, arguments, image)
        {
            Url = url;
        }

        public MenuItem(string text, string arguments, string image, string url, string target)
            : this(text, arguments, image, url)
        {
            Target = target;
        }
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            MenuItem oClone = new MenuItem();

            foreach (PropertyInfo oInfo in GetType().GetProperties())
            {
                if (oInfo.Name.Eq("Items"))
                    continue;

                oInfo.SetValue(oClone, oInfo.GetValue(this, null), null);
            }

            foreach (MenuItem oItem in Items)
            {
                oClone.Items.Add((MenuItem)oItem.Clone());
            }

            return oClone;
        }

        #endregion

        #region properties
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Command { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Text { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Arguments { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Image { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Url { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Target { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public MenuItemType Type { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public string ToolTip { get; set; }
        [Browsable(false)]
        public bool Selected { get; set; }
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool Enabled { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty), MergableProperty(false)]
        public MenuItemCollection Items
        {
            get { return items; }
            set { items = value; Items.Owner = this; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty), MergableProperty(false)]
        public List<Script> ScriptEvents { get; set; }
        [Browsable(false), Bindable(false)]
        public MenuItem Parent { get; internal set; }
        [Browsable(false), Bindable(false)]
        public bool IsRootElement { get { return Parent == null; } }
        [Browsable(false), Bindable(false)]
        public int Depth { get { return Parent == null ? 1 : (Parent.Depth + 1); } }
        [Browsable(false), Bindable(false)]
        public int Index { get { return Container.IndexOf(this); } }
        /// <summary>
        /// Get or Set the reference to the MenuItemCollection
        /// </summary>
        internal MenuItemCollection Container { get; set; }
        #endregion
    }
}
